<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item of hotcity" :key="item.id">
            <div class="button" @click="handleCityClick(item.name)">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, index) of city" :key="index" >
        <div class="title border-topbottom" :ref="item.initial">{{item.initial}}</div>
        <div class="item" v-for="(lcc,index) of item.list" :key="index">
          <div
            class="item-list border-bottom"
            @click="handleCityClick(lcc.name)"
          >{{lcc.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: ['city', 'hotcity', 'letter'],
  methods: {
    handleCityClick (city) {
      this.chengeCity(city)
      this.$router.push('/')
    },
    ...mapMutations(['chengeCity'])
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  watch: {
    letter () {
      if (this.letter) {
        const elenment = this.$refs[this.letter][0]
        this.scroll.scrollToElement(elenment)
      }
    }
  },
  mounted () {
    this.scroll = new Bscroll(this.$refs.wrapper)
  }
}
</script>

<style lang="stylus" scoped>
    .list
      font-size: .26rem
      color: #666
      position: absolute
      top: 1.57rem
      left: 0
      right: 0
      bottom: 0
      overflow: hidden
    .border-topbottom
      &:before
        border-color: #ccc
      &:after
        border-color: #ccc
    .border-bottom
      &:before
        border-color: #ccc
    .title
      background: #eee
      padding-left: .2rem
      line-height: .54rem
    .button-list
      padding: .1rem 0.6rem .1rem .1rem
      overflow: hidden
      .button-wrapper
        float: left
        width: 33.33%
        .button
          margin: .1rem
          text-align: center
          border: .02rem solid #ccc
          padding: .1rem 0
          border-radius: .06rem
    .item
      line-height: .76rem
      .item-list
        padding-left: .2rem
</style>
